package com.memory.api.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.memory.api.mapper.UserInterfaceInfoMapper;
import com.memory.api.service.InterfaceInfoService;
import com.memory.api.service.UserInterfaceInfoService;
import com.memory.api.service.UserService;
import com.memory.common.common.ErrorCode;
import com.memory.common.exception.BusinessException;
import com.memory.common.model.entity.InterfaceInfo;
import com.memory.common.model.entity.User;
import com.memory.common.model.entity.UserInterfaceInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lenovo
 * @description 针对表【user_interface_info(用户调用接口关系)】的数据库操作Service实现
 * @createDate 2023-08-23 14:29:17
 */
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo> implements UserInterfaceInfoService {

    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private UserService userService;

    /**
     * 接口调用统计
     *
     * @param interfaceInfoId 接口 id
     * @param userId          用户 id
     * @return 是否调用成功
     */
    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        // 判断
        if (interfaceInfoId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 接口调用次数+1
        UpdateWrapper<InterfaceInfo> interfaceInfoUpdateWrapper = new UpdateWrapper<>();
        interfaceInfoUpdateWrapper.setSql("count = count + 1");
        if (!(interfaceInfoService.update(interfaceInfoUpdateWrapper))) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用次数+1, 出错了");
        }
        // 用户积分-1
        User user = userService.getById(userId);
        if (user == null || user.getBalance() <= 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败, 用户积分不足");
        }
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.setSql("balance = balance - 1");
        if (!(userService.update(userUpdateWrapper))) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用次数+1, 出错了");
        }

        // 调用成功
        return true;
    }

    /**
     * 列举该用户调用记录
     *
     * @param userId 用户 id
     * @return 接口记录
     */
    @Override
    public List<UserInterfaceInfo> listByUserId(long userId) {
        QueryWrapper<UserInterfaceInfo> interfaceInfoQueryWrapper = new QueryWrapper<>();
        interfaceInfoQueryWrapper.eq("userId", userId);

        List<UserInterfaceInfo> userInterfaceInfoList = this.list(interfaceInfoQueryWrapper);
        if (userInterfaceInfoList == null) {
            return Lists.newArrayList();
        }
        return userInterfaceInfoList;
    }
}




